package ru.usu.gv.gui.analysis;

import ru.usu.gv.core.GraphCanvas;
import ru.usu.gv.styles.StyleFacade;
import ru.usu.gv.uicomponents.UIEdge;
import ru.usu.gv.uicomponents.UIGraph;

/**
 @author Sergey Pupyrev 
 12.04.2007
 */

public class UniformEdgeAnalyzer extends GraphAnalyzer
{
	public UniformEdgeAnalyzer()
	{
		super(AnalysisPanel.getInstance().getUniformEdgeSlider());
	}

	@Override
	protected double calculateAnalysValue()
	{
		double standardLength = StyleFacade.getEdgeLength();
		UIGraph uiGraph = GraphCanvas.getInstance().getCurrentGraph();

		int countEdges = uiGraph.getEdges().size();
		double error = 0.0;
		for (UIEdge edge : uiGraph.getEdges())
		{
			double len = edge.getLength();
			double delta = Math.abs(len - standardLength) / standardLength;
			if (delta > 1.0)
				delta = Math.sqrt(delta);
			error += delta;
		}

		error /= countEdges;
		return 1.0 - error;
	}
}
